<template>
  <el-dialog v-bind="$attrs"
             :visible.sync="AdddialogVisible"
             v-on="$listeners"
             title="字典添加"
             :before-close="modalClose">
    <el-form ref="字典添加" :model="formData" :rules="rules" size="medium" label-width="100px"
             label-position="left">
      <el-form-item label="字典名称" prop="dictName">
        <el-input v-model="formData.dictName" placeholder="请输入字典名称" :maxlength="10" clearable
                  :style="{width: '100%'}"></el-input>
      </el-form-item>


      <el-form-item label="字典编码" prop="dictCode">
        <el-input v-model="formData.dictCode" placeholder="请输入字典编码" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="formData.description" type="textarea" placeholder="请输入描述"
                  :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="字典类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择字典类型" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in typeOptions" :key="index" :label="item.label" :value="item.value"
                     :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="handelConfirm">确定</el-button>
    </div>
  </el-dialog>
</template>

<style>

</style>

<script>

import axios from "axios"
export default {
  data () {
    return {
      msg: "..",
      AdddialogVisible:true,
      formData: {
        dictName: undefined,
        dictCode: undefined,
        description: undefined,
        type: undefined,
      },
      rules: {
        dictName: [{
          required: true,
          message: '请输入字典名称',
          trigger: 'blur'
        }],
        dictCode: [{
          required: true,
          message: '请输入字典编码',
          trigger: 'blur'
        }],
        description: [{
          required: true,
          message: '请输入描述',
          trigger: 'blur'
        }],
        type: [{
          required: true,
          message: '请选择字典类型',
          trigger: 'change'
        }],
      },
      typeOptions: [{
        "label": "字符串",
        "value": 0
      }, {
        "label": "数字",
        "value": 1
      }],


    }
  },
  props: ["closeAdd","init","admin"],
  methods:{
    modalClose(){
      this.close();
    },
    onClose() {

      this.$refs['字典添加'].resetFields()
    },
    close() {
      this.$emit('update:visible', false)
    },

    //点击确定

    /**
     * 待修改,因登录存储方式未知,用假数据添加创建人和修改人
     */
    handelConfirm(){

      this.$refs['字典添加'].validate(valid => {
        if (!valid) return
      console.log(this.formData)
      axios.post("http://localhost:8102/dict", {
        dictName: this.formData.dictName,
        dictCode: this.formData.dictCode,
        description: this.formData.description,
        type: this.formData.type,
        createBy: this.admin.adminId,
        updateBy:this.admin.adminId
      }).then((data)=>{
        this.$message({
          showClose: true,
          message: '添加成功',
          type: 'success'
        });

      }).catch((data)=>{
        this.$message({
          showClose: true,
          message: '未知原因,请联系管理员',
          type: 'error'
        });
      });
        this.init();
        this.closeAdd();
      })
    }
  }
}
</script>
